//
//  RTMApiManager.m
//  RTMKitDemo
//
//  Created by reyzhang on 2021/12/30.
//

#import "RTMApiManager.h"

//登录
NSString *const UserLogin = @"chat/login";


@implementation RTMApiManager

/**
 拼接真实的请求地址
 */
+ (NSString *)realURL:(NSString *)url {
    NSString *baseURL = @"https://im.yimaxiaoerlang.cn/";
    if (!baseURL.length || baseURL.length == 0) {
        NSLog(@"未配置接口服务器地址");
        NSAssert(baseURL.length > 0, @"未配置接口服务器地址,应用启动后请在RZIMConfig类中设置");
    }
    NSString *lastChar = [baseURL substringFromIndex:baseURL.length -1];
    NSString *requestUrl;
    if ([lastChar isEqualToString:@"/"]) {
        requestUrl = [baseURL stringByAppendingString:url];
    }else {
        requestUrl = [baseURL stringByAppendingFormat:@"/%@",url];
    }
//    requestUrl = [requestUrl stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
    NSLog(@"real request : %@",requestUrl);
    return requestUrl;
}

/**
 登录
 */
+ (void)userLoginWithUserId:(NSString *)userId
                       name:(NSString *)name
                    success:(RTMSuccessBlock)successBlock
                    failure:(RTMFailureBlock)failureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"uid"] = userId;
    params[@"username"] = name;
    params[@"appKey"] = kYMAppKey;
    params[@"appSecret"] = kYMAppSecret;
    params[@"type"] = @"2";
    [NetworkUtils postjson:[self realURL:UserLogin] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !successBlock ?: successBlock(dic);
        
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        
        !failureBlock ?: failureBlock(error);
    }];
}




@end
